package com.recharge.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.recharge.common.util.LockUtil;
import com.recharge.common.util.RedissonLocker;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RedissonConfig {

    /**
     * RedissonClient,单机模式
     *
     * @return
     * @throws IOException
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {
        Config config = new Config();
        SentinelServersConfig serverConfig = config.useSentinelServers()
                //.addSentinelAddress("redis://172.19.175.36:26379", "redis://172.19.175.37:26379", "redis://172.19.175.40:26379")
                .addSentinelAddress("redis://127.0.0.1:26379", "redis://127.0.0.1:26379", "redis://127.0.0.1:26379")
                .setMasterName("mymaster")
                .setReadMode(ReadMode.SLAVE)
                .setTimeout(20000);
        return Redisson.create(config);
    }

    @Bean
    public RedissonLocker redissonLocker(RedissonClient redissonClient) {
        RedissonLocker locker = new RedissonLocker(redissonClient);
        //设置LockUtil的锁处理对象
        LockUtil.setLocker(locker);
        return locker;
    }
}